<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ChannelLog;
use App\Models\Channel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ChannelLogController extends AdminController
{

    protected $title = '启动详情';

    protected $description = '测试';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $channel_id = request()->get('channel_id') ?? 0;

        $channel = Channel::with('logs')->find($channel_id);

        return Grid::make(new ChannelLog(), function (Grid $grid) use ($channel) {

            $grid->model()->where('channel_id', $channel ? $channel->id : 0);

            $grid->column('index', '次数序号')->display(function () {
                return $this->index ?? '';
            });
            $grid->column('start_time', '启动时间');
            $grid->column('end_time', '结束时间');
            $grid->column('usage_seconds', '时长计算')->display(function () {
                if (!$this->end_time) return '';
                return intval($this->usage_seconds / 60) . '分钟';
            });

            if ($channel) {


                $startup_count = $channel->logs()->count() ?? 0;
                $total_usage_seconds = $channel->logs()->sum('usage_seconds');

                $grid->title('简介 ')
                    ->description(
                        "appid:                           ". $channel->appid .
                        '                          |中文标识:                           ' . $channel->chinese_sign .
                        '                          |总计启动次数:                           ' . $startup_count .
                        '                          |总计使用时长:                           ' . (($total_usage_seconds) ? intval($total_usage_seconds / 60) . '分钟': 0)
                    );

            }
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->toolsWithOutline(false);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ChannelLog(), function (Show $show) {
            $show->field('id');
            $show->field('channel_id');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ChannelLog(), function (Form $form) {
            $form->display('id');
            $form->text('channel_id');
            $form->text('start_time');
            $form->text('end_time');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
